/*
 * @Author: mengxiangji
 * @Email: 397082664@qq.com
 * @Date: 2020-07-02 14:05:27
 * @Last Modified by: mxj
 * @Last Modified time: 2020-07-22 14:23:14
 * @Description: 备课小作业提交情况
 */

//使用方法：传递课程参数即可生成。
//RouteTransition(HomeworkStudentList(preLessonId: "990c544d2dba40b4a30958b1f0712fb0"),rans: "slide").pushTransRoute(context);

import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/model_data/homeworkclass/homeworkclass.dart';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';

import 'homework_review.dart';

class HomeworkStudentList extends BaseWidget {
  String preLessonId;

  HomeworkStudentList({Key key, this.preLessonId}) : super(key: key);

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _HomeworkStudentListState();
  }
}

class _HomeworkStudentListState extends BaseWidgetState<HomeworkStudentList> {
  var _classlist = [];
  String selectclassid = "";
  @override
  Widget buildWidget(BuildContext context) {
    List<Widget> childs = [];

    Widget titlewidget = GestureDetector(
        child: Container(
            padding: EdgeInsets.fromLTRB(0, 7, 0, 6),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  SFSymbols.tray_arrow_down_fill,
                  size: 14.0,
                  color: Theme.of(context).accentColor,
                ),
                Text(" 查看课程信息",
                    style: TextStyle(
                        color: Theme.of(context).primaryTextTheme.caption.color,
                        fontSize: 14.0))
              ],
            )),
        onTap: () {
          Navigator.pop(context);
        });

    _classlist.forEach((element) {
      childs.add(getClassListItem(context, element));
    });

    return Container(
      padding: EdgeInsets.fromLTRB(24, 0, 24, 0),
      width: double.infinity,
      color: Colors.transparent,
      child: Column(
        children: <Widget>[
          titlewidget,
          Expanded(
              child: Container(
                  width: double.infinity,
                  child: ListView(
                    children: childs,
                  )))
        ],
      ),
    );
  }

  Widget getClassListItem(BuildContext context, dynamic item) {
    Widget classStatsContainer = GestureDetector(
        child: Container(
            height: 72.0,
            width: double.infinity,
            color: Colors.transparent,
            child: Column(children: <Widget>[
              Container(
                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                  width: double.infinity,
                  color: Colors.transparent,
                  child: Text(item.name,
                      overflow: TextOverflow.ellipsis,
                      softWrap: false,
                      textAlign: TextAlign.left,
                      style: TextStyle(
                          color:
                              Theme.of(context).primaryTextTheme.caption.color,
                          fontSize: 18.0))),
              Row(children: <Widget>[
                Container(
                    color: Colors.transparent,
                    child: Text(
                      '打开人数' + item.openNumb.toString() + '人',
                      style: TextStyle(
                          color: Theme.of(context)
                              .primaryTextTheme
                              .headline5
                              .color,
                          fontSize: 14.0),
                    )),
                Container(
                    color: Colors.transparent,
                    child: Text(
                      ' | 提交人数' + item.submitNumb.toString() + '人',
                      style: TextStyle(
                          color:
                              Theme.of(context).primaryTextTheme.caption.color,
                          fontSize: 14.0),
                    )),
                Container(
                    color: Colors.transparent,
                    child: Text(
                      ' | 提交率' + item.submitRate,
                      style: TextStyle(
                          color:
                              Theme.of(context).primaryTextTheme.caption.color,
                          fontSize: 14.0),
                    )),
              ])
            ])),
        onTap: () {
          setState(() {
            this.selectclassid = item.id.toString();
          });
        });
    Widget linecontainer = Container(
        margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
        height: 1,
        width: double.infinity,
        color: Theme.of(context).dividerColor,
        child: Text(''));
    List<Widget> _getChild() {
      List<Widget> childern = <Widget>[];
      for (int i = 0; i < item.studentList.length; i++) {
        Widget w;
        if (item.studentList[i].status == 1) {
          w = Container(
            padding: EdgeInsets.fromLTRB(16, 3, 16, 3),
            decoration: new BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(30)),
              border: Border.all(
                  width: 1,
                  color: Theme.of(context).primaryTextTheme.headline5.color),
            ),
            child: Text(
              item.studentList[i].name,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 12.0,
                color: Theme.of(context).primaryTextTheme.headline5.color,
              ),
            ),
          );
        } else if (item.studentList[i].status == 0) {
          w = Container(
            padding: EdgeInsets.fromLTRB(16, 3, 16, 3),
            decoration: new BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(30)),
              border:
                  Border.all(width: 1, color: Theme.of(context).cursorColor),
            ),
            child: Text(
              item.studentList[i].name,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 12.0,
                color: Theme.of(context).primaryTextTheme.headline2.color,
              ),
            ),
          );
        } else if (item.studentList[i].status == 2) {
          w = Container(
            padding: EdgeInsets.fromLTRB(16, 3, 16, 3),
            decoration: new BoxDecoration(
              color: Theme.of(context).primaryTextTheme.headline5.color,
              borderRadius: BorderRadius.all(Radius.circular(30)),
              border: Border.all(
                  width: 1,
                  color: Theme.of(context).primaryTextTheme.headline5.color),
            ),
            child: Text(
              item.studentList[i].name,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 12.0,
                color: Theme.of(context).primaryTextTheme.button.color,
              ),
            ),
          );
        } else if (item.studentList[i].status == 3) {
          w = Container(
            padding: EdgeInsets.fromLTRB(16, 3, 16, 3),
            decoration: new BoxDecoration(
              color: Theme.of(context).primaryTextTheme.headline5.color,
              borderRadius: BorderRadius.all(Radius.circular(30)),
              border: Border.all(
                  width: 1,
                  color: Theme.of(context).primaryTextTheme.headline5.color),
            ),
            child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
              Text(
                item.studentList[i].name + " ",
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 12.0,
                  color: Theme.of(context).primaryTextTheme.button.color,
                ),
              ),
              Icon(
                SFSymbols.checkmark_seal_fill,
                size: 14.0,
                color: Theme.of(context).accentColor,
              )
            ]),
          );
        }
        if (w != null) {
          childern.add(GestureDetector(
            child: w,
            onTap: () {
              if (item.studentList[i].studentHomeworkId != "") {
                RouteTransition(HomeworkReview(
                  studentHomeworkId: item.studentList[i].studentHomeworkId,
                  classId: item.id.toString(),
                )).pushTransRoute(context);
              }
            },
          ));
        }
      }
      return childern;
    }

    return Container(
        width: double.infinity,
        color: Colors.transparent,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            classStatsContainer,
            linecontainer,
            Offstage(
                offstage: selectclassid != item.id.toString(),
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 14, 0, 14),
                  child: Wrap(
                    spacing: 6,
                    runSpacing: 13,
                    children: _getChild(),
                  ),
                )),
          ],
        ));
  }

  //获取小作业班级学生数据
  _getHomeworkClassList() async {
    var _homeworkClassData = await G.req.homeworkClassData
        .getBookMenuList(preLessonId: widget.preLessonId);

    setState(() {
      _classlist.clear();
      if (_homeworkClassData.data['classList'].length > 0) {
        _classlist.addAll(_homeworkClassData.data["classList"]
            .map<HomeworkClass>((e) => HomeworkClass.fromJson(e))
            .toList());
        selectclassid = _classlist[0].id.toString();
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _getHomeworkClassList();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void onCreate() {
    // TODO: implement onCreate
  }

  @override
  void onPause() {
    // TODO: implement onPause
  }

  @override
  void onResume() {
    // TODO: implement onResume
  }
}
